//
//  CommentTableViewCell.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/22.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "CommentTableViewCell.h"

#import <AVFoundation/AVFoundation.h>

#define LABEL_TEXTCOLOR RGB_COLOR(80, 80, 80, 1)

@interface CommentTableViewCell ()

@property (nonatomic, strong) AVPlayer * recordPlayer; // 录音播放

@end

@implementation CommentTableViewCell

#pragma mark 🌷🌷🌷life cycle🌷🌷🌷
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = RGB_COLOR(239, 239, 239, 1);
        
        // 修改选中效果
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        // 添加视图
        [self addSubview:self.headImageView];
        [self addSubview:self.contentLabel];
        [self addSubview:self.playRecordButton];
        [self addSubview:self.nameLabel];
        [self addSubview:self.releaseDateLabel];
        [self addSubview:self.commentButton];
        [self addSubview:self.grayLineView];
        
        // 自动布局
        [_headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.offset(SCREEN_HEIGHT * 0.05);
            make.top.offset(SCREEN_HEIGHT * 0.018);
            make.left.offset(SCREEN_WIDTH * 0.023);
        }];
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self).multipliedBy(0.954);
            make.left.equalTo(_headImageView);
            make.top.offset(SCREEN_HEIGHT * 0.095);
        }];
        [_playRecordButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(self).multipliedBy(0.25);
            make.width.equalTo(_playRecordButton.mas_height).multipliedBy(6.55);
            make.left.equalTo(_headImageView);
            make.top.offset(SCREEN_HEIGHT * 0.095);
        }];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(SCREEN_WIDTH * 0.08);
            make.top.offset(SCREEN_HEIGHT * 0.014);
        }];
        [_releaseDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_nameLabel);
            make.bottom.equalTo(_headImageView);
        }];
        [_commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(SCREEN_WIDTH * 0.05);
            make.height.equalTo(_commentButton.mas_width).multipliedBy(0.6);
            make.centerY.equalTo(_contentLabel);
            make.right.offset(SCREEN_WIDTH * -0.08);
        }];
        [_grayLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.sizeOffset(CGSizeMake(SCREEN_WIDTH, 1));
            make.centerX.equalTo(self);
            make.bottom.offset(0);
        }];
    }
    return self;
}

#pragma mark 🌷🌷🌷event responds🌷🌷🌷
- (void)respondsToCommentButton {
    if (_clickBlock) {
        _clickBlock(_nameLabel.text);
    }
}
- (void)respondsToPlayRecordButton {
    [self.recordPlayer play];
}

#pragma mark 🌷🌷🌷getters and setters🌷🌷🌷
- (UIImageView *)headImageView {
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] init];
    }
    return _headImageView;
}
- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.numberOfLines = 2;
        _contentLabel.font = [UIFont systemFontOfSize:25];
    }
    return _contentLabel;
}
- (UIButton *)playRecordButton {
    if (!_playRecordButton) {
        _playRecordButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_playRecordButton setImage:[UIImage imageNamed:@"voice_back.png"] forState:UIControlStateNormal];
        [_playRecordButton addTarget:self action:@selector(respondsToPlayRecordButton) forControlEvents:UIControlEventTouchUpInside];
        
        // 声音图标
        UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"voice_play.png"]];
        [_playRecordButton addSubview:imageView];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(_playRecordButton).multipliedBy(0.7);
            make.width.equalTo(imageView.mas_height).multipliedBy(0.67);
            make.centerY.equalTo(_playRecordButton);
            make.centerX.equalTo(_playRecordButton).multipliedBy(1.7);
        }];
    }
    return _playRecordButton;
}
- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = [UIFont systemFontOfSize:25];
    }
    return _nameLabel;
}
- (UILabel *)releaseDateLabel {
    if (!_releaseDateLabel) {
        _releaseDateLabel = [[UILabel alloc] init];
        _releaseDateLabel.textColor = LABEL_TEXTCOLOR;
        _releaseDateLabel.font = [UIFont systemFontOfSize:18];
    }
    return _releaseDateLabel;
}
- (UIButton *)commentButton {
    if (!_commentButton) {
        _commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_commentButton setImage:[UIImage imageNamed:@"chat_reply.png"] forState:UIControlStateNormal];
        [_commentButton addTarget:self action:@selector(respondsToCommentButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _commentButton;
}
- (UIView *)grayLineView {
    if (!_grayLineView) {
        _grayLineView = [[UIView alloc] init];
        _grayLineView.backgroundColor = RGB_COLOR(154, 154, 154, 1);
    }
    return _grayLineView;
}
- (AVPlayer *)recordPlayer {
    if (!_recordPlayer) {
        AVPlayerItem * item = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:BASEURLAPPEND(_recordUrl)]];
        _recordPlayer = [AVPlayer playerWithPlayerItem:item];
    }
    return _recordPlayer;
}

@end
